// class Dequeue {
//     constructor () {
//       this.count = 0
//       this.lowestCount = 0
//       this.items = {}
//     }
// }

class Dequeue {
    constructor () {
      this.count = 0
      this.lowestCount = 0
      this.items = {}
    }
    size () {
      return this.count - this.lowestCount
    }
    isEmpty () {
      return this.size() === 0
    }
    clear () {
      this.count = 0
      this.lowestCount = 0
      this.items = {}
    }
    toString () {
      if (this.isEmpty()) {
        return ''
      }
      let strObj = this.items[this.lowestCount]
      for (let i = this.lowestCount + 1; i < this.count; i++) {
        strObj = `${strObj},${this.items[i]}`
      }
      return strObj
    }
    addFront (element) {
      if (this.isEmpty()) {
        this.addBack(element)
      } else {
        this.items[--this.lowestCount] = element
      }
    }
    addBack (element) {
      this.items[this.count] = element
      this.count++
    }
    removeFront () {
      if (this.isEmpty()) {
        return undefined
      }
      const result = this.items[this.lowestCount]
      delete this.items[this.lowestCount]
      this.lowestCount++
      return result
    }
    removeBack () {
      if (this.isEmpty()) {
        return undefined
      }
      this.count--
      const result = this.items[this.count]
      delete this.items[this.count]
      return result
    }
    peekFront () {
      if (this.isEmpty()) {
        return undefined
      }
      return this.items[this.lowestCount]
    }
    peekBack () {
      if (this.isEmpty()) {
        return undefined
      }
      return this.items[this.count - 1]
    }
}


const dequeue = new Dequeue()
console.log(dequeue.isEmpty())  // true
dequeue.addBack('AAA')
dequeue.addBack('BBB')
console.log(dequeue.toString()) // AAA,BBB

dequeue.addBack('CCC')
console.log(dequeue.toString()) // AAA,BBB,CCC
console.log(dequeue.size())     // 3
dequeue.removeFront()
console.log(dequeue.toString()) // BBB,CCC
dequeue.removeBack()
console.log(dequeue.toString()) // BBB
dequeue.addFront('DDD')
console.log(dequeue.peekFront())// DDD
console.log(dequeue.peekBack()) // BBB
console.log(dequeue.toString()) // DDD,BBB